using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;

public class DateAndDayOfWeek : MonoBehaviour
{
    private TMP_Text dateText;
    private DateTime currentDate;
    private string formattedDateAndDayOfWeek;
    private StringBuilder stringBuilder = new StringBuilder();

    private void Awake()
    {
        dateText = GetComponent<TMP_Text>();
        currentDate = DateTime.Now;
        UpdateDateAndDayOfWeek();
    }

    private void Update()
    {
        DateTime now = DateTime.Now;
        if (now.Date != currentDate.Date)
        {
            currentDate = now;
            UpdateDateAndDayOfWeek();
        }

        UpdateTime(now);
    }

    private void UpdateDateAndDayOfWeek()
    {
        // 格式化日期为 "yyyy-MM-dd" 格式
        string formattedDate = currentDate.ToString("yyyy-MM-dd");

        // 获取当前是星期几
        DayOfWeek currentDayOfWeek = currentDate.DayOfWeek;

        // 将 DayOfWeek 转换为对应的中文星期几
        string chineseDayOfWeek = ConvertDayOfWeekToChinese(currentDayOfWeek);

        // 组合日期和星期几的信息
        formattedDateAndDayOfWeek = formattedDate + " " + chineseDayOfWeek;
    }

    private void UpdateTime(DateTime time)
    {
        // 格式化时间为 HH:mm:ss 的形式
        string timeString = time.ToString("HH:mm:ss");

        // 使用 StringBuilder 拼接字符串
        stringBuilder.Clear();
        stringBuilder.Append(formattedDateAndDayOfWeek);
        stringBuilder.Append(" ");
        stringBuilder.Append(timeString);

        // 更新文本对象的显示内容
        dateText.text = stringBuilder.ToString();
    }

    // 将 DayOfWeek 转换为中文星期几
    private string ConvertDayOfWeekToChinese(DayOfWeek dayOfWeek)
    {
        string[] chineseDaysOfWeek = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        return chineseDaysOfWeek[(int)dayOfWeek];
    }
}
